package cn.ssm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.ssm.biz.impl.BanZhuRenBizImpl;
import cn.ssm.biz.impl.ClassroomBizImpl;
import cn.ssm.biz.impl.ComputerroomBizImpl;
import cn.ssm.biz.impl.CoursesBizImpl;
import cn.ssm.biz.impl.PeriodBizImpl;
import cn.ssm.biz.impl.TeacherBizImpl;
import cn.ssm.pojo.Class;
import cn.ssm.pojo.Courses;
import cn.ssm.pojo.Pager;
import cn.ssm.pojo.Period;
import cn.ssm.pojo.Teacher;
import cn.ssm.query.CoursesQuery;

/**
 * 查看维修
 * 
 * @author shi
 *
 */
@Controller
@RequestMapping("/courses")
public class CoursesController {
	@Autowired
	@Qualifier("computerroomBizImpl")
	private ComputerroomBizImpl computerroomBizImpl;


	@Autowired
	@Qualifier("coursesBizImpl")
	private CoursesBizImpl coursesBizImpl;


	@Autowired
	@Qualifier("TeacherBizImpl")
	private TeacherBizImpl teacherBizImpl;

	@Autowired
	@Qualifier("ClassroomBizImpl")
	private ClassroomBizImpl classroomBizImpl;

	@Autowired
	@Qualifier("BanZhuRenBizImpl")
	private BanZhuRenBizImpl BanZhuRenBizImpl;
	
	@Autowired
	@Qualifier("PeriodBizImpl")
	private PeriodBizImpl PeriodBizImpl;
	
	@RequestMapping("/findAddList")
	public String findAddList(ModelMap mo){
		List<Teacher> teacherList = teacherBizImpl.getTeacherList1();
		List<Class> classroomInfo = classroomBizImpl.classroomInfo1();
		List<Period> periodList = PeriodBizImpl.periodInfo1();
		
		mo.put("teacher",teacherList);

		mo.put("classroomInfo", classroomInfo);
		mo.put("periodList", periodList);
		return "addCourses";
	}
	
	@RequestMapping("/addCourses")
	public String addCourses(Courses course,ModelMap mo,@RequestParam("periodId") Integer periodId,@RequestParam("teacherId") Integer teacherId,@RequestParam("classroomId") Integer classroomId){
		System.out.println("addcoouses=====================================");
		String url=null;
		course.setPeroidId(periodId);
		course.setTeacherId(teacherId);
		course.setClassroomId(classroomId);
		int num=coursesBizImpl.insertCourse(course);
		if (num>0){
			System.out.println("===添加成功=1=================");
			url="forward:/courses/findCoursesQueryList";
		}
		return url;
	}
	@RequestMapping("/delCourses")
	public String delCourses(@RequestParam("id") int id){
		String url=null;
		int num=coursesBizImpl.delCoueseById(id);
		if (num>0){
//			url="coursesList";
			url="forward:/courses/findCoursesQueryList";
		}
		return url;
		
	}

	@RequestMapping("/findCoursesQueryList")
	public String findCoursesQueryList(Model model,HttpServletRequest request){
		String url="";
		String currentPageStr =request.getParameter("currentPage");
		//System.out.println(currentPageStr);
		int rowCount = coursesBizImpl.selCourseCount();
		System.out.println("num:"+rowCount);
		Pager pager=null;
		if(currentPageStr==null){
			pager=new Pager(rowCount,3,1);
		}else{
			int currentPage=Integer.parseInt(request.getParameter("currentPage"));
			pager=new Pager(rowCount, 3, currentPage);
		}
		pager.setUrl("/courses/findCoursesQueryList");
		model.addAttribute("pager", pager);
		
		List<CoursesQuery> coursesList=coursesBizImpl.selCoursesQueryInfo(pager);
		
		if(coursesList!=null){
			model.addAttribute("coursesList", coursesList);
			
			System.out.println("==================="+coursesList.size());
			url="coursesList";
			System.out.println("coursesList"+coursesList.size());
		}else{
			url="404";
		}
		return url;
//		List<Teacher> teacherList = teacherBizImpl.getTeacherList1();
//		List<Class> classroomInfo = classroomBizImpl.classroomInfo1();
//		List<Period> periodList = PeriodBizImpl.periodInfo1();
//		List<BanZhuRen> banZhuRen=BanZhuRenBizImpl.getBanzhurenInfo();
//		List<Computerroom> computerroom=computerroomBizImpl.getRoomdescribe();
//		mo.put("teacher",teacherList);
//		mo.put("banZhuRen",banZhuRen);
//		mo.put("computerroom",computerroom);
//		mo.put("classroomInfo", classroomInfo);
//		mo.put("periodList", periodList);
//		mo.put("coursesList", coursesList);
//		if(coursesList!=null){
//			mo.put("coursesList", coursesList);
//			url="coursesList";
//			System.out.println("coursesList"+coursesList.size());
//		}else{
//			url="404";
//		}
//		return url;
//		
		
	}
	@RequestMapping("/findUpdateList")
	public String findUpdateList(Integer id,ModelMap mo){

		CoursesQuery coursesList= coursesBizImpl.selCourseById(id);
		List<Teacher> teacherList = teacherBizImpl.getTeacherList1();
		List<Class> classroomInfo = classroomBizImpl.classroomInfo1();
		List<Period> periodList = PeriodBizImpl.periodInfo1();
		
		mo.put("teacherList",teacherList);
		mo.put("classroomInfo", classroomInfo);
		mo.put("periodList", periodList);
		
		
		String url="";
		mo.put("cqu", coursesList);
		if(coursesList!=null){
			mo.addAttribute("cqu", coursesList);
			url="updateCourses";
		}else{
			url="404";
		}
		return url;
		
	}
	
	@RequestMapping("/updateCourses")
	public String updateCourses(Courses courses){
		
		int num=coursesBizImpl.updateCourseById(courses);
		String url=" ";
		if(num > 0){
			url="forward:/courses/findCoursesQueryList";
		}else{
			url="404";
		}
		return url;
	
		
	}
}
